package com.mstanford.movie.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mstanford.movie.dao.MovieDao;
import com.mstanford.movie.utils.ConstantUtil;
import com.mstanford.movie.utils.DataUtil;

public class QueryPlayServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		
		String movieName = request.getParameter("movieName");
		
		String today = request.getParameter("today");
		String tomorrow = request.getParameter("tomorrow");
		
		String editionAll = request.getParameter("editionAll");
		String edition1 = request.getParameter("edition1");
		String edition2 = request.getParameter("edition2");
		String edition3 = request.getParameter("edition3");
		
		String timeAll = request.getParameter("timeAll");
		String timeAm = request.getParameter("timeAm");
		String timePm = request.getParameter("timePm");
		String timeNight = request.getParameter("timeNight");
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("movieName", movieName);
		map.put("edition", "1,2,3");
		map.put("time", "all");
		
		if(!DataUtil.isEmpty(today) && !DataUtil.isEmpty(tomorrow)) {
			map.put("date", "'" + today + "','" + tomorrow + "'");
		} else if(!DataUtil.isEmpty(today)) {
			map.put("date", "'" + today + "'");
		} else {
			map.put("date", "'" + tomorrow + "'");
		}
		
		if(!DataUtil.isEmpty(editionAll) || (!DataUtil.isEmpty(edition1) && !DataUtil.isEmpty(edition2) && !DataUtil.isEmpty(edition3))) {
			map.put("edition", "1,2,3"); // IMAX 2D 3D
		} else if(!DataUtil.isEmpty(edition1) && !DataUtil.isEmpty(edition2)) {
			map.put("edition", "1,2"); // IMAX 2D
		} else if(!DataUtil.isEmpty(edition1) && !DataUtil.isEmpty(edition3)) {
			map.put("edition", "1,3"); // IMAX 3D
		} else if(!DataUtil.isEmpty(edition2) && !DataUtil.isEmpty(edition3)) {
			map.put("edition", "2,3"); // 2D 3D
		} else if(!DataUtil.isEmpty(edition1)) {
			map.put("edition", "1"); // IMAX
		} else if(!DataUtil.isEmpty(edition2)) {
			map.put("edition", "2"); // 2D
		} else if(!DataUtil.isEmpty(edition3)) {
			map.put("edition", "3"); // 3D
		}
		
		if(!DataUtil.isEmpty(timeAll) || (!DataUtil.isEmpty(timeAm) && !DataUtil.isEmpty(timePm) && !DataUtil.isEmpty(timeNight))) {
			map.put("time", "all"); // 早中晚
		} else if(!DataUtil.isEmpty(timeAm) && !DataUtil.isEmpty(timePm)) {
			map.put("time", "ap"); // 早中
		} else if(!DataUtil.isEmpty(timeAm) && !DataUtil.isEmpty(timeNight)) {
			map.put("time", "an"); // 早晚
		} else if(!DataUtil.isEmpty(timePm) && !DataUtil.isEmpty(timeNight)) {
			map.put("time", "pn"); // 中晚
		} else if(!DataUtil.isEmpty(timeAm)) {
			map.put("time", "am"); // 早
		} else if(!DataUtil.isEmpty(timePm)) {
			map.put("time", "pm"); // 中
		} else if(!DataUtil.isEmpty(timeNight)) {
			map.put("time", "night"); // 晚
		}
		
		MovieDao dao = new MovieDao();
		List<Map<String, String>> list = dao.queryPlayByCond(map);
		
		for(Map<String, String> _map : list) {
			String playId = _map.get("playId");
			int ticket = dao.queryTicketCountByPlayId(playId);
			int ticketNum = ConstantUtil.TICKET_COUNT - ticket;
			
			if(ticketNum == 0) {
				_map.put("ticket", "无");
			} else {
				_map.put("ticket", "余" + ticketNum);
			}
		}
		
		String json = DataUtil.listToJson(list);
		
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		out.print(json);
		out.flush();
		out.close();
	}
}